{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3fdd31e0-9b4c-483f-885b-4ba1275a320d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple'}\n",
      "{'orange', 'pear', 'banana', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "#先创建对象再加入元素。\n",
    "#在创建空集合的时候只能使用s = set()，因为s = {}创建的是空字典\n",
    "#直接把一堆元素用花括号括起来{元素1, 元素2, ..., 元素n}。\n",
    "#重复元素在set中会被自动被过滤。\n",
    "basket = set()\n",
    "basket.add('apple')\n",
    "basket.add('banana')\n",
    "print(basket)  # {'banana', 'apple'}\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)  # {'banana', 'apple', 'pear', 'orange'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "91a5a6f9-9ef0-47c7-9708-0fa4ff4dabff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'Google', 'Lsgogroup', 'Taobao'}\n",
      "{'Google', 'Lsgogroup', 'Taobao'}\n"
     ]
    }
   ],
   "source": [
    "#使用set(value)工厂函数，把列表或元组转换成集合。\n",
    "a = set('abracadabra')\n",
    "print(a)  \n",
    "# {'b', 'd', 'r', 'c', 'a'}\n",
    "\n",
    "b = set((\"Google\", \"Lsgogroup\", \"Taobao\", \"Taobao\"))\n",
    "print(b)  \n",
    "# {'Taobao', 'Lsgogroup', 'Google'}\n",
    "\n",
    "c = set([\"Google\", \"Lsgogroup\", \"Taobao\", \"Google\"])\n",
    "print(c)  \n",
    "# {'Taobao', 'Lsgogroup', 'Google'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c7b8e78d-510a-426c-88ba-a498f3574541",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5]\n",
      "[0, 1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "lst = [0, 1, 2, 3, 4, 5, 5, 3, 1]\n",
    "\n",
    "temp = []\n",
    "for item in lst:\n",
    "    if item not in temp:\n",
    "        temp.append(item)\n",
    "\n",
    "print(temp)  # [0, 1, 2, 3, 4, 5]\n",
    "\n",
    "a = set(lst)\n",
    "print(list(a))  # [0, 1, 2, 3, 4, 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e3b31d3a-57c6-4d40-85bf-6af05fe9bd4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "s = set(['Google', 'Baidu', 'Taobao'])\n",
    "print(len(s))  # 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e331a577-d3dc-4307-8798-8968e5979d5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Google\n",
      "Baidu\n",
      "Taobao\n"
     ]
    }
   ],
   "source": [
    "s = set(['Google', 'Baidu', 'Taobao'])\n",
    "for item in s:\n",
    "    print(item)\n",
    "    \n",
    "# Baidu\n",
    "# Google\n",
    "# Taobao"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6447e0b7-1727-497b-af8f-b333aa0442d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s = set(['Google', 'Baidu', 'Taobao'])\n",
    "print('Taobao' in s)  # True\n",
    "print('Facebook' not in s)  # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "99abab7d-6573-45e7-9981-57a0a228a7d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'orange', 'banana', 'apple'}\n",
      "{'cherry', 'orange', 'banana', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "# et.add(elmnt)用于给集合添加元素，如果添加的元素在集合中已存在，则不执行任何操作。\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "fruits.add(\"orange\")\n",
    "print(fruits)  \n",
    "# {'orange', 'cherry', 'banana', 'apple'}\n",
    "\n",
    "fruits.add(\"apple\")\n",
    "print(fruits)  \n",
    "# {'orange', 'cherry', 'banana', 'apple'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "82fc6632-110b-4527-aca4-d6524ddff981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'google', 'baidu', 'banana', 'apple'}\n",
      "{'lsgo', 'google', 'apple', 'baidu', 'dreamtech'}\n"
     ]
    }
   ],
   "source": [
    "# set.update(set)用于修改当前集合，可以添加新的元素或集合到当前集合中，如果添加的元素在集合中已存在，则该元素只会出现一次，重复的会忽略。\n",
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"baidu\", \"apple\"}\n",
    "x.update(y)\n",
    "print(x)\n",
    "# {'cherry', 'banana', 'apple', 'google', 'baidu'}\n",
    "\n",
    "y.update([\"lsgo\", \"dreamtech\"])\n",
    "print(y)\n",
    "# {'lsgo', 'baidu', 'dreamtech', 'apple', 'google'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1d93c270-b502-4c02-bc0b-89d53ef0a1f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "# set.remove(item) 用于移除集合中的指定元素。如果元素不存在，则会发生错误。\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "fruits.remove(\"banana\")\n",
    "print(fruits)  # {'apple', 'cherry'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "deefb5da-872a-4784-a940-21b493913702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "# set.discard(value) 用于移除指定的集合元素。remove() 方法在移除一个不存在的元素时会发生错误，而 discard() 方法不会。\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "fruits.discard(\"banana\")\n",
    "fruits.discard(\"banana\")\n",
    "print(fruits)  # {'apple', 'cherry'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "88070a95-eea5-40ef-909c-a936e8e47a5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple'}\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "# set.pop() 用于随机移除一个元素。\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "x = fruits.pop()\n",
    "print(fruits)  # {'cherry', 'apple'}\n",
    "print(x)  # banana"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a2c9feef-59b0-454e-bcc5-631c0eb620a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'm', 'c', 'a'}\n",
      "{'a', 'c'}\n",
      "{'a', 'c'}\n",
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'a', 'c'}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "由于 set 是无序和无重复元素的集合，所以两个或多个 set 可以做数学意义上的集合操作。\n",
    "\n",
    "set.intersection(set1, set2) 返回两个集合的交集。\n",
    "set1 & set2 返回两个集合的交集。\n",
    "set.intersection_update(set1, set2) 交集，在原始的集合上移除不重叠的元素。\n",
    "'''\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)  # {'r', 'a', 'c', 'b', 'd'}\n",
    "print(b)  # {'c', 'a', 'l', 'm', 'z'}\n",
    "\n",
    "c = a.intersection(b)\n",
    "print(c)  # {'a', 'c'}\n",
    "print(a & b)  # {'c', 'a'}\n",
    "print(a)  # {'a', 'r', 'c', 'b', 'd'}\n",
    "\n",
    "a.intersection_update(b)\n",
    "print(a)  # {'a', 'c'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a66b5be6-6bf3-4bbe-872a-95d677797246",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'm', 'c', 'a'}\n",
      "{'l', 'z', 'b', 'd', 'm', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'b', 'd', 'm', 'r', 'c', 'a'}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "set.union(set1, set2) 返回两个集合的并集。\n",
    "set1 | set2 返回两个集合的并集。\n",
    "'''\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)  # {'r', 'a', 'c', 'b', 'd'}\n",
    "print(b)  # {'c', 'a', 'l', 'm', 'z'}\n",
    "\n",
    "print(a | b)  \n",
    "# {'l', 'd', 'm', 'b', 'a', 'r', 'z', 'c'}\n",
    "\n",
    "c = a.union(b)\n",
    "print(c)  \n",
    "# {'c', 'a', 'd', 'm', 'r', 'b', 'z', 'l'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b529876f-8664-43b8-b9f4-8ced7e6c0729",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'm', 'c', 'a'}\n",
      "{'b', 'd', 'r'}\n",
      "{'b', 'd', 'r'}\n",
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'b', 'd', 'r'}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "set.difference(set) 返回集合的差集。\n",
    "set1 - set2 返回集合的差集。\n",
    "set.difference_update(set) 集合的差集，直接在原来的集合中移除元素，没有返回值。\n",
    "'''\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)  # {'r', 'a', 'c', 'b', 'd'}\n",
    "print(b)  # {'c', 'a', 'l', 'm', 'z'}\n",
    "\n",
    "c = a.difference(b)\n",
    "print(c)  # {'b', 'd', 'r'}\n",
    "print(a - b)  # {'d', 'b', 'r'}\n",
    "\n",
    "print(a)  # {'r', 'd', 'c', 'a', 'b'}\n",
    "a.difference_update(b)\n",
    "print(a)  # {'d', 'r', 'b'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1ee6b6f7-534d-4a5d-a06d-41bea6df956b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'm', 'c', 'a'}\n",
      "{'l', 'z', 'd', 'b', 'm', 'r'}\n",
      "{'l', 'z', 'd', 'b', 'm', 'r'}\n",
      "{'b', 'd', 'r', 'c', 'a'}\n",
      "{'l', 'z', 'b', 'd', 'm', 'r'}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "set.symmetric_difference(set)返回集合的异或。\n",
    "set1 ^ set2 返回集合的异或。\n",
    "set.symmetric_difference_update(set)移除当前集合中在另外一个指定集合相同的元素，并将另外一个指定集合中不同的元素插入到当前集合中。\n",
    "'''\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)  # {'r', 'a', 'c', 'b', 'd'}\n",
    "print(b)  # {'c', 'a', 'l', 'm', 'z'}\n",
    "\n",
    "c = a.symmetric_difference(b)\n",
    "print(c)  # {'m', 'r', 'l', 'b', 'z', 'd'}\n",
    "print(a ^ b)  # {'m', 'r', 'l', 'b', 'z', 'd'}\n",
    "\n",
    "print(a)  # {'r', 'd', 'c', 'a', 'b'}\n",
    "a.symmetric_difference_update(b)\n",
    "print(a)  # {'r', 'b', 'm', 'l', 'z', 'd'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "af1644cf-0790-4004-b9d0-3701d4d75c4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# set.issubset(set)判断集合是不是被其他集合包含，如果是则返回 True，否则返回 False。\n",
    "# set1 <= set2 判断集合是不是被其他集合包含，如果是则返回 True，否则返回 False。\n",
    "x = {\"a\", \"b\", \"c\"}\n",
    "y = {\"f\", \"e\", \"d\", \"c\", \"b\", \"a\"}\n",
    "z = x.issubset(y)\n",
    "print(z)  # True\n",
    "print(x <= y)  # True\n",
    "\n",
    "x = {\"a\", \"b\", \"c\"}\n",
    "y = {\"f\", \"e\", \"d\", \"c\", \"b\"}\n",
    "z = x.issubset(y)\n",
    "print(z)  # False\n",
    "print(x <= y)  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "480b2e48-d6c7-4ab1-a0bd-665631db4c3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# set.issuperset(set)用于判断集合是不是包含其他集合，如果是则返回 True，否则返回 False。\n",
    "# set1 >= set2 判断集合是不是包含其他集合，如果是则返回 True，否则返回 False。\n",
    "x = {\"f\", \"e\", \"d\", \"c\", \"b\", \"a\"}\n",
    "y = {\"a\", \"b\", \"c\"}\n",
    "z = x.issuperset(y)\n",
    "print(z)  # True\n",
    "print(x >= y)  # True\n",
    "\n",
    "x = {\"f\", \"e\", \"d\", \"c\", \"b\"}\n",
    "y = {\"a\", \"b\", \"c\"}\n",
    "z = x.issuperset(y)\n",
    "print(z)  # False\n",
    "print(x >= y)  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3db50adb-b34a-4487-87e1-0246302c1bf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# set.isdisjoint(set) 用于判断两个集合是不是不相交，如果是返回 True，否则返回 False。\n",
    "x = {\"f\", \"e\", \"d\", \"c\", \"b\"}\n",
    "y = {\"a\", \"b\", \"c\"}\n",
    "z = x.isdisjoint(y)\n",
    "print(z)  # False\n",
    "\n",
    "x = {\"f\", \"e\", \"d\", \"m\", \"g\"}\n",
    "y = {\"a\", \"b\", \"c\"}\n",
    "z = x.isdisjoint(y)\n",
    "print(z)  # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7cb9ee2f-b1e1-42a7-a63c-ccf8e78717f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3} <class 'set'>\n",
      "[0, 1, 2, 3] <class 'list'>\n",
      "(0, 1, 2, 3) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "se = set(range(4))\n",
    "li = list(se)\n",
    "tu = tuple(se)\n",
    "\n",
    "print(se, type(se))  # {0, 1, 2, 3} <class 'set'>\n",
    "print(li, type(li))  # [0, 1, 2, 3] <class 'list'>\n",
    "print(tu, type(tu))  # (0, 1, 2, 3) <class 'tuple'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "51962c3e-55a7-4a5b-a470-3abb5fe1cc83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})\n",
      "frozenset({'l', 'r', 'u', 'o', 's', 'p', 'g'})\n"
     ]
    }
   ],
   "source": [
    "# Python 提供了不能改变元素的集合的实现版本，即不能增加或删除元素，类型名叫frozenset。需要注意的是frozenset仍然可以进行集合操作，只是不能用带有update的方法。\n",
    "# frozenset([iterable]) 返回一个冻结的集合，冻结后集合不能再添加或删除任何元素。\n",
    "a = frozenset(range(10))  # 生成一个新的不可变集合\n",
    "print(a)  \n",
    "# frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})\n",
    "\n",
    "b = frozenset('lsgogroup')\n",
    "print(b)  \n",
    "# frozenset({'g', 's', 'p', 'r', 'u', 'o', 'l'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "197e6087-5df5-4041-a8e1-321fc3db0aa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "['I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p']\n",
      "[1, 1, 2, 3, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "# list(sub) 把一个可迭代对象转换为列表。\n",
    "a = list()\n",
    "print(a)  # []\n",
    "\n",
    "b = 'I Love LsgoGroup'\n",
    "b = list(b)\n",
    "print(b)  \n",
    "# ['I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p']\n",
    "\n",
    "c = (1, 1, 2, 3, 5, 8)\n",
    "c = list(c)\n",
    "print(c)  # [1, 1, 2, 3, 5, 8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7e51212c-d1d1-43f4-800f-64a72043c810",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "('I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p')\n",
      "(1, 1, 2, 3, 5, 8)\n"
     ]
    }
   ],
   "source": [
    "# tuple(sub) 把一个可迭代对象转换为元组。\n",
    "a = tuple()\n",
    "print(a)  # ()\n",
    "\n",
    "b = 'I Love LsgoGroup'\n",
    "b = tuple(b)\n",
    "print(b)  \n",
    "# ('I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p')\n",
    "\n",
    "c = [1, 1, 2, 3, 5, 8]\n",
    "c = tuple(c)\n",
    "print(c)  # (1, 1, 2, 3, 5, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "af00a3b3-ee26-4181-8974-194aa64f1c73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "# str(obj) 把obj对象转换为字符串\n",
    "a = 123\n",
    "a = str(a)\n",
    "print(a)  # 123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "67b6800c-5ea9-4e2b-a5b0-ae6ef21761b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "16\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "# len(s) 返回对象（字符、列表、元组等）长度或元素个数。\n",
    "a = list()\n",
    "print(len(a))  # 0\n",
    "\n",
    "b = ('I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p')\n",
    "print(len(b))  # 16\n",
    "\n",
    "c = 'I Love LsgoGroup'\n",
    "print(len(c))  # 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "492e8f6a-51fe-4410-8e09-891b82bc58e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "99\n",
      "v\n"
     ]
    }
   ],
   "source": [
    "# max(sub)返回序列或者参数集合中的最大值\n",
    "print(max(1, 2, 3, 4, 5))  # 5\n",
    "print(max([-8, 99, 3, 7, 83]))  # 99\n",
    "print(max('IloveLsgoGroup'))  # v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "69fc4265-011e-4038-9401-de53436459c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "-8\n",
      "G\n"
     ]
    }
   ],
   "source": [
    "# min(sub)返回序列或参数集合中的最小值\n",
    "print(min(1, 2, 3, 4, 5))  # 1\n",
    "print(min([-8, 99, 3, 7, 83]))  # -8\n",
    "print(min('IloveLsgoGroup'))  # G"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "64905efe-24cb-4423-bc9f-bc6cf84ac0f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "35\n",
      "25\n",
      "45\n"
     ]
    }
   ],
   "source": [
    "# sum(iterable[, start=0]) 返回序列iterable与可选参数start的总和。\n",
    "print(sum([1, 3, 5, 7, 9]))  # 25\n",
    "print(sum([1, 3, 5, 7, 9], 10))  # 35\n",
    "print(sum((1, 3, 5, 7, 9)))  # 25\n",
    "print(sum((1, 3, 5, 7, 9), 20))  # 45"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2e96b1ab-74d3-4f6c-9414-1cd00a7e9a8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-8, 3, 7, 83, 99]\n",
      "[99, 83, 7, 3, -8]\n",
      "[{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "sorted(iterable, key=None, reverse=False) 对所有可迭代的对象进行排序操作。\n",
    "iterable -- 可迭代对象。\n",
    "key -- 主要是用来进行比较的元素，只有一个参数，具体的函数的参数就是取自于可迭代对象中，指定可迭代对象中的一个元素来进行排序。\n",
    "reverse -- 排序规则，reverse = True 降序 ， reverse = False 升序（默认）。\n",
    "返回重新排序的列表。\n",
    "'''\n",
    "x = [-8, 99, 3, 7, 83]\n",
    "print(sorted(x))  # [-8, 3, 7, 83, 99]\n",
    "print(sorted(x, reverse=True))  # [99, 83, 7, 3, -8]\n",
    "\n",
    "t = ({\"age\": 20, \"name\": \"a\"}, {\"age\": 25, \"name\": \"b\"}, {\"age\": 10, \"name\": \"c\"})\n",
    "x = sorted(t, key=lambda a: a[\"age\"])\n",
    "print(x)\n",
    "# [{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "08e0dd96-ee28-448d-9468-c345757da047",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'reversed'>\n",
      "<reversed object at 0x000001A8240555A0>\n",
      "['p', 'u', 'o', 'r', 'g', 'o', 'g', 's', 'l']\n",
      "['p', 'u', 'o', 'r', 'g', 'o', 'g', 's', 'l']\n",
      "[8, 7, 6, 5]\n",
      "[83, 7, 3, 99, -8]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "reversed(seq) 函数返回一个反转的迭代器。\n",
    "seq -- 要转换的序列，可以是 tuple, string, list 或 range。\n",
    "'''\n",
    "s = 'lsgogroup'\n",
    "x = reversed(s)\n",
    "print(type(x))  # <class 'reversed'>\n",
    "print(x)  # <reversed object at 0x000002507E8EC2C8>\n",
    "print(list(x))\n",
    "# ['p', 'u', 'o', 'r', 'g', 'o', 'g', 's', 'l']\n",
    "\n",
    "t = ('l', 's', 'g', 'o', 'g', 'r', 'o', 'u', 'p')\n",
    "print(list(reversed(t)))\n",
    "# ['p', 'u', 'o', 'r', 'g', 'o', 'g', 's', 'l']\n",
    "\n",
    "r = range(5, 9)\n",
    "print(list(reversed(r)))\n",
    "# [8, 7, 6, 5]\n",
    "\n",
    "x = [-8, 99, 3, 7, 83]\n",
    "print(list(reversed(x)))\n",
    "# [83, 7, 3, 99, -8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e7be5fe9-809f-472d-9f44-5ee2750b9156",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n",
      "[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]\n",
      "0,Spring\n",
      "1,Summer\n",
      "2,Fall\n",
      "3,Winter\n"
     ]
    }
   ],
   "source": [
    "# enumerate(sequence, [start=0])\n",
    "#【例子】用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。\n",
    "\n",
    "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n",
    "a = list(enumerate(seasons))\n",
    "print(a)  \n",
    "# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n",
    "\n",
    "b = list(enumerate(seasons, 1))\n",
    "print(b)  \n",
    "# [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]\n",
    "\n",
    "for i, element in a:\n",
    "    print('{0},{1}'.format(i, element))\n",
    "# 0,Spring\n",
    "# 1,Summer\n",
    "# 2,Fall\n",
    "# 3,Winter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "96a06c45-119f-4e67-9530-f1e9a2d64476",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x000001A825D80980>\n",
      "[(1, 4), (2, 5), (3, 6)]\n",
      "[(1, 4), (2, 5), (3, 6)]\n",
      "[1, 2, 3]\n",
      "[4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "zip(iter1 [,iter2 [...]])\n",
    "用于将可迭代的对象作为参数，将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的对象，这样做的好处是节约了不少的内存。\n",
    "我们可以使用 list() 转换来输出列表。\n",
    "如果各个迭代器的元素个数不一致，则返回列表长度与最短的对象相同，利用 * 号操作符，可以将元组解压为列表。\n",
    "'''\n",
    "\n",
    "a = [1, 2, 3]\n",
    "b = [4, 5, 6]\n",
    "c = [4, 5, 6, 7, 8]\n",
    "\n",
    "zipped = zip(a, b)\n",
    "print(zipped)  # <zip object at 0x000000C5D89EDD88>\n",
    "print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]\n",
    "zipped = zip(a, c)\n",
    "print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]\n",
    "\n",
    "a1, a2 = zip(*zip(a, b))\n",
    "print(list(a1))  # [1, 2, 3]\n",
    "print(list(a2))  # [4, 5, 6]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "370cb2aa-d4b6-496d-94ed-fbef9b1c620e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
